#include <linux/slab.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/vmalloc.h>

MODULE_LICENSE("GPL");
static int __init kzalloc_init(void);
static void __exit kzalloc_exit(void);

#define MEM_KZALLOC_SIZE 8092
#define MEM_VMALLOC_SIZE 8092

char * mem_spvm;
char * mem_spvm_vm;

int __init kzalloc_init(void)
{
	// 调用kzalloc分配一个内存空间
	mem_spvm = (char *)kzalloc(MEM_KZALLOC_SIZE, GFP_KERNEL);
	
	if(mem_spvm == NULL )
		printk("kzalloc failed! \n");
	else
	{
		printk("kzalloc successfully! addr = 0x%lx\n", (unsigned long)mem_spvm );
		printk("the content of mem_spvm+2 is: %d\n", *(mem_spvm+2));
		printk("the content of mem_spvm+500 is: %d\n", *(mem_spvm+500));
	}


	mem_spvm_vm = (char *)vmalloc(MEM_VMALLOC_SIZE);
	
	if(mem_spvm_vm == NULL )
		printk("vmalloc failed! \n");
	else
		printk("vmalloc successfully! addr = 0x%lx\n", (unsigned long)mem_spvm_vm );

	return 0;
}

void __exit kzalloc_exit(void)
{
	if(mem_spvm != NULL)
	{
		kfree(mem_spvm); //释放由kzalloc( )所分配的内存空间
		printk("kfree ok! \n");
	}

	
	if(mem_spvm_vm != NULL)
	{
		kfree(mem_spvm_vm); //释放由vmalloc( )所分配的内存空间
		printk("kfree ok! \n");
	}

	printk("exit! \n");
}

module_init(kzalloc_init);
module_exit(kzalloc_exit);
